/**
 * PS现金收支报表管理初始化
 */
var CashExpenditure = {
    id: "CashExpenditureTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
	width:"2000px"
};

/**
 * 初始化表格的列
 */
CashExpenditure.initColumn = function () {
    return [
        [
            {field: 'selectItem', radio: true,colspan: 1,rowspan: 2,width:'36px'},
            {title: '日期', field: 'revDate', visible: true,width:'100px', align: 'center', valign: 'middle',colspan: 1,rowspan: 2,
							formatter:function(value,row,index){
                        return value.substring(0,10);
				}
			},
            {title: '摘要', field: 'note', visible: true, align: 'center', valign: 'middle',width:'120px',colspan: 1,rowspan: 2},
			{title: '往来客户', field: 'customerName', visible: true,width:'100px', align: 'center', valign: 'middle',colspan: 1,rowspan: 2},
            {title: '1、收入金额', field: 'money', visible: true,width:'120px', halign:'center', align: 'right', valign: 'middle',colspan: 1,rowspan: 2,
            formatter:function(value,row,index){ if(row.type==1){return value;}else{ return '';}}},
            {title: '2、支出金额', field: 'money', visible: true,width:'120px', halign:'center', align: 'right', valign: 'middle',colspan: 1,rowspan: 2,
            formatter:function(value,row,index){ if(row.type==2){return value;}else{ return '';}}},
            {title: '余额(PS)', field: 'balance', visible: true,width:'120px',halign:'center', align: 'right', valign: 'middle',colspan: 1,rowspan: 2},
            {title: '成本支出项目', visible: true,halign:'center', align: 'center', valign: 'middle',colspan: 11,rowspan: 1,width:"1500px"},

        ],
        [
            {title: '2.1工资薪酬', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==1){return value;}else{ return '';}}},
            {title: '2.2食堂及补贴', field: 'money',visible: true,width:'200px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==3){return value;}else{ return '';}}},
            {title: '2.3房租费用', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==4){return value;}else{ return '';}}},
            {title: '2.4水费', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==5){return value;}else{ return '';}}},
            {title: '2.5电费', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==6){return value;}else{ return '';}}},
            {title: '2.6网络费用', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==7){return value;}else{ return '';}}},
            {title: '2.7机票费用', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==8){return value;}else{ return '';}}},
            {title: '2.8签证及保关费用', field: 'money',visible: true,width:'200px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==9){return value;}else{ return '';}}},
            {title: '2.9办公费用', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.detailsId==10){return value;}else{ return '';}}},
            {title: '2.10其他杂费', field: 'money',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.type==11){return value;}else{ return '';}}},
            {title: '支付小计', field: 'payoutTatol',visible: true,width:'100px', halign:'center', align: 'right', valign: 'middle'},
        ]
    ];
};

/**
 * 检查是否选中
 */
CashExpenditure.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CashExpenditure.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加现金收支报表
 */
CashExpenditure.openAddCashExpenditure = function () {
    var index = layer.open({
        type: 2,
        title: '添加PS现金收支报表',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/cashExpenditure/cashExpenditure_add'
    });
    this.layerIndex = index;
};


/**
 * 添加期初余额
 */
CashExpenditure.openAddBalance = function () {
    var index = layer.open({
        type: 2,
        title: '添加PS现金收支期初余额',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/cashExpenditure/cashExpenditure_addBalance'
    });
    this.layerIndex = index;
};

/**
 * 打开查看现金收支报表详情
 */
CashExpenditure.openCashExpenditureDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: 'PS现金收支报表详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/cashExpenditure/cashExpenditure_update/' + CashExpenditure.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除现金收支报表
 */
CashExpenditure.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/cashExpenditure/delete", function (data) {
            Feng.success("删除成功!");
            CashExpenditure.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("cashExpenditureId",this.seItem.id);
        ajax.start();
    }
};

/**
* 打开导出页面
*/
CashExpenditure.operExport = function(){
    window.location.href = "/cashExpenditure/load?beginDate=" + $("#beginDate").val()+"&endDate="+$("#endDate").val();
};

CashExpenditure.resetSearch = function () {
    $("#platform").val("");
    $("#type").val("");
    $("#beginDate").val("");
    $("#endDate").val("");
}

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
CashExpenditure.formParams = function() {
    var queryData = {};
    queryData['platform'] = $("#platform").val();
    queryData['type'] = $("#type").val();
    queryData['beginDate'] = $("#beginDate").val();
    queryData['endDate'] = $("#endDate").val();
    return queryData;
}



/**
 * 查询现金收支报表列表
 */
CashExpenditure.search = function () {
    CashExpenditure.table.refresh({query: CashExpenditure.formParams()});
    $("#CashExpenditureSum").remove();
    $("#CashExpenditureTable").after(CashExpenditure.tatol())
};

CashExpenditure.tatol= function(){

    var tatol;
    var ajax = new $ax(Feng.ctxPath + "/cashExpenditure/total", function (result) {
        tatol=result;
    }, function (result) {
        Feng.error("查询失败!" + result.responseJSON.message + "!");
    });
    ajax.setData(CashExpenditure.formParams());
    ajax.start();
    var rowTem = '<div><table id="CashExpenditureSum" data-mobile-responsive="true" data-click-to-select="false" class="table table-hover table-striped" style="margin-top: -0px;color: #2A2E36;font-size: 14px;"><tbody id="CashExpenditureSum" >'+
        '<tr data-index="0">'+
        '<td style="text-align: center; vertical-align: middle; width:356px;">总计：</td> '+
        '<td style="text-align: right; vertical-align: middle;width:120px">'+tatol.incomeTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:120px">'+tatol.outTotal+'</td> '+
        '<td style="text-align: right; vertical-align: middle;width:120px "></td>'+
        '<td style="text-align: right; vertical-align: middle; width:136px">'+tatol.payrollTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.foodTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.rentTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.waterTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.elecTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.netTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.ticketTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.visaTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.officeTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.otherTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle;width:136px">'+tatol.allTotal+'</td>'+
        '</tr></tbody></table></div>';
    return rowTem;
}

$(function () {
    var defaultColunms = CashExpenditure.initColumn();
    var table = new BSTable(CashExpenditure.id, "/cashExpenditure/list", defaultColunms,CashExpenditure);
    table.setPaginationType("server");
    table.setQueryParams(CashExpenditure.formParams());
    table.setShowColumns(false);
    table.setShowRefresh(false);
    CashExpenditure.table = table.init();
    //添加表头
    $(".fixed-table-toolbar").after("<div style='text-align: center;vertical-align: middle;font-size: 22px;color: #6979d7;font-weight: bold;'> PS收入/支出明细分类表</div>");
    $("#CashExpenditureTable").after(CashExpenditure.tatol());
});
